package com.eastfair.projectcore.controller;

import com.eastfair.projectcore.dto.*;
import com.eastfair.projectcore.entity.Menu;
import com.eastfair.projectcore.enumeration.MenuTerminalTypeEnum;
import com.eastfair.projectcore.service.MenuService;
import com.eastfair.projectcore.vo.MenuVO;
import com.eastfair.log.point.PointUtil;
import com.eastfair.point.UserLoginPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * 菜单
 * </p>
 *
 * @author ligang
 * @date 2021-07-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/menu")
@Api(value = "Menu", tags = "菜单")
public class MenuController extends SuperController<MenuService, Long, Menu, MenuPageQuery, MenuSaveDTO, MenuUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    PointUtil pointUtil;
    @Override
    public void handlerResult(IPage<Menu> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Menu> menuList = list.stream().map((map) -> {
            Menu menu = Menu.builder().build();
            //TODO 请在这里完成转换
            return menu;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(menuList));
    }


    /**
     * 删除,并删除旗下所有子菜单
     * @param ids 集合
     */
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/del")
    public R del(@RequestBody List<Long> ids) {
        if(ids!=null && !ids.isEmpty()){
            for(Long id : ids){
                baseService.del(id);
            }
        }
        return R.success();
    }


    /**
     * 查询菜单信息和全部子信息
     * @param id  菜单id
     * @return  查询结果
     */
    @ApiOperation(value = "查询菜单信息和全部子信息", notes = "查询菜单信息和全部子信息")
    @GetMapping("/findAllById")
    public R<MenuVO> findAllById(Long id) {
        return success(baseService.findAllById(id));
    }


    /**
     * 查询菜单全部子信息
     * @param id 菜单id
     * @return 查询结果
     */
    @ApiOperation(value = "查询菜单全部子信息", notes = "查询菜单全部子信息")
    @GetMapping("/findAllChildOfparentId")
    public R<List<MenuVO>> findAllChildOfparentId(Long id) {
        MenuVO menuVO = baseService.findAllById(id);
        return success(menuVO.getChildVoList());
    }



    /**
     * 创建菜单节点
     * @param menuDTO 菜单实体
     */
    @ApiOperation(value = "创建菜单节点", notes = "创建菜单节点")
    @PostMapping("/createMenu")
    public R createMenu(@RequestBody @Validated({MenuDTO.createMenu.class}) MenuDTO menuDTO) {
        MenuSaveDTO menuSaveDTO = new MenuSaveDTO();
        BeanUtils.copyProperties(menuDTO, menuSaveDTO);
        R<Menu> r = baseService.createMenu(menuSaveDTO);
        return r;
    }

    /**
     * 修改菜单信息
     * @param menuUpdateDTO 菜单更新实体
     */
    @ApiOperation(value = "修改菜单信息", notes = "修改菜单信息")
    @PutMapping("/updMenu")
    public R updMenu(@RequestBody MenuUpdateDTO menuUpdateDTO) {
        return baseService.updateMenu(menuUpdateDTO);
    }


    /**
     * 置顶
     * @param id 菜单id
     */
    @ApiOperation(value = "置顶", notes = "置顶")
    @PostMapping("/sortTop")
    public R sortTop(@RequestBody Long id) {
        baseService.sortTop(id);
        return R.success();
    }

    /**
     * 查询菜单信息和全部子信息
     * @param menuDTO 菜单实体
     * @return 查询结果
     */
    @ApiOperation(value = "通过子菜单查询菜单信息和全部子信息", notes = "通过子菜单查询菜单信息和全部子信息")
    @PostMapping("/findAllByChildId")
    public R<List<MenuVO>> findAllByChildId(@RequestBody MenuDTO menuDTO) {
        List<Long> ids = new ArrayList<>();
        if (menuDTO != null && menuDTO.getMenuIds() != null) {
            ids = menuDTO.getMenuIds();
        }
        return success(baseService.findAllByChildId(ids));
    }

    /**
     * 查询左侧菜单信息(包含菜单，目录)
     * @param id  菜单id
     * @return  查询结果
     */
    @ApiOperation(value = "查询菜单信息和全部子信息", notes = "查询菜单信息和全部子信息")
    @GetMapping("/findAllByChildIdNoButton")
    public R<List<MenuVO>> findAllByChildIdNoButton(Long id) {
        MenuVO menuVO =  baseService.findAllByIdNoButton(id);
        return success(menuVO.getChildVoList());
    }



    /**
     * 查询左侧菜单信息(包含菜单，目录)
     * @param id  菜单id
     * @return  查询结果
     */
    @ApiOperation(value = "查询菜单信息和全部子信息", notes = "查询菜单信息和全部子信息")
    @GetMapping("/findAllByIdNoButton")
    public R<MenuVO> findAllByIdNoButton(Long id) {
        return success(baseService.findAllByIdNoButton(id));
    }

    /**
     * 查询按钮权限信息集合
     * @return  查询结果
     */
    @ApiOperation(value = "查询按钮权限信息集合", notes = "查询按钮权限信息集合")
    @GetMapping("/findButtonPermission")
    public R<List<String>> findButtonPermission() {
        return success(baseService.findButtonPermission());
    }


    /**
     * 查询菜单的权限标识
     * @param menuDTO 菜单实体
     * @return 查询结果
     */
    @ApiOperation(value = "查询菜单的权限标识", notes = "查询菜单的权限标识")
    @PostMapping("/findPermissionAllByChildId")
    public R<List<String>> findPermissionAllByChildId(@RequestBody MenuDTO menuDTO) {
        List<Long> ids = new ArrayList<>();
        if (menuDTO != null && menuDTO.getMenuIds() != null) {
            ids = menuDTO.getMenuIds();
        }
        return success(baseService.findPermissionAllByChildId(ids));
    }

}
